#include "contenttype.h"
#include "util.h"

CONTENT_TYPE*   
contenttype_new()
{
    CONTENT_TYPE*   ctype;

    ctype = (CONTENT_TYPE*)emalloc(sizeof(CONTENT_TYPE));
    if (NULL == ctype)
        return NULL;

    memset(ctype, 0, sizeof(CONTENT_TYPE));

    return ctype;
}


void            
contenttype_delete(CONTENT_TYPE*  ctype)
{
    if (NULL == ctype)
        return ;

    contenttype_clear(ctype);

    efree(ctype);

}

void            
contenttype_clear(CONTENT_TYPE*  ctype)
{
    if (NULL == ctype)
        return ;

    SAFE_FREE(ctype->Type)
    SAFE_FREE(ctype->Name)
    SAFE_FREE(ctype->CharSet)
    SAFE_FREE(ctype->Boundary)

}

int
contenttype_set(void* type, char* member, char* value)
{
    CONTENT_TYPE* ctype = type;
    if (NULL == ctype)
        return -1;
    if (NULL == member)
        return -2;
    if (NULL == value)
        return -3;

    switch(member[0])
    {
    case 't': // type
        if (NULL != ctype->Type)
            efree(ctype->Type);
        ctype->Type = xstrdup(value);
        break;
    case 'n':
        if (NULL != ctype->Name)
            efree(ctype->Name);
        ctype->Name = xstrdup(value);
        break;
    case 'c': // charset
        if (NULL != ctype->CharSet)
            efree(ctype->CharSet);
        ctype->CharSet = xstrdup(value);
        break;
    case 'b': //boundary
        if (NULL != ctype->Boundary)
            efree(ctype->Boundary);
        ctype->Boundary = xstrdup(value);
        break;
    default:
        break;
    }
    
    return 0;
}

int             
contenttype_parse(CONTENT_TYPE*  ctype, char* data)
{
    int     ret;

    if (NULL == ctype)
        return -1;

    if (NULL == data)
        return -2;
    
    contenttype_clear(ctype);   
   
    ret = fields_get_pairs(data, "type", ctype, contenttype_set);

    return ret;
}


void 
contenttype_print(CONTENT_TYPE*  ctype)
{
    if (NULL == ctype)
        return;

    printf("Content-Type\n");
    printf("\tType: %s\n", ctype->Type);
    printf("\tName: %s\n", ctype->Name);
    printf("\tCharSet: %s\n", ctype->CharSet);
    printf("\tBoundary: %s\n", ctype->Boundary);
}

int
contenttype_string(CONTENT_TYPE *ctype, char *save, int slen)
{
    if (NULL == ctype)
        return -1;
    
    if (NULL == ctype->Type)
        return -2;

    
    return 0;
}


#ifdef MAIN_TEST_CT

GHashTable  *gmemhash;
int main()
{
    int     ret;
    char    *test = "Content-Type: multipart/mixed;boundary=\"---------=_plZefGJyzGprTPgpIiimnMLgnWyAdYYQkwVqdF\";charset=\"gb2312\"";
    char    *test1 = "Content-Type: multipart/related;\n\ttype=\"multipart/alternative\";\n\tboundary=\"----=_NextPart_000_0006_01C6C780.DFD5CD30\"";
    char    *test2 = "multipart/mixed; boundary=\"0-108943514-1143442624=:744\"";
    CONTENT_TYPE*  ctype;

    gmemhash = e_init(NULL);
 
    ctype = contenttype_new();

    ret = contenttype_parse(ctype, test2);
    
    printf("ret=%d\n", ret);
    contenttype_print(ctype);

    e_destroy(gmemhash);
    return 0;
}
#endif


